home *** CD-ROM | disk | FTP | other *** search
- Path: news.gate.net!stpfl2-14
- From: rmcinnis@gate.net (Robert McInnis)
- Newsgroups: comp.lang.c++
- Subject: Re: Callback Functions In C++
- Date: Mon, 15 Apr 96 04:08:56 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4ksinf$ink@news.gate.net>
- References: <4kdrfu$kiu@morse.ukonline.co.uk>
- NNTP-Posting-Host: stpfl2-14.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4kdrfu$kiu@morse.ukonline.co.uk>, andy.walsh@ukonline.co.uk wrote:
- >Can anyone tell me how I could code a callback function that works
- >inside a class and that I can pass the address of to the waveInOpen
- >function. It would seem that a member function carries an extra
- >'this' pointer and so cannot be addressed the same as an ordinary
- >function. Any ideas? I also tried to use Borland's response table
- >to handle the messages but it will only respond to a 'SendMessage'
- >command and not to my low level audio commands.
- >
-
- What you want to do is code a static in your class that has the following
- signature:
-
- static void CALLBACK WaveProc( HWAVE hW, UINT msg, DWORD inst,
- DWORD P1, DWORD P2 ) ;
-
- According to the Win32 docs, this is what you get:
-
- HWAVE hW, // handle of waveform device
- UINT msg, // sent message
- DWORD inst, // instance data
- DWORD P1, // application-defined parameter
- DWORD P2 // application-defined parameter
-
- I would probably forget about P1 && P2, I haven't a clue, off the top of my
- head, how to set them. The things you can do in this function, or any
- functions it calls, is restricted to certain Windows API calls. The only API
- functions you can call are:
-
- EnterCriticalSection PostThreadMessage
- LeaveCriticalSection SetEvent
- midiOutLongMsg timeGetSystemTime
- midiOutShortMsg timeGetTime
- OutputDebugString timeKillEvent
- PostMessage timeSetEvent
-
-
- Next, register the function. This is the key:
-
- waveInOpen( lphWaveIn, IDDevice, lpwf, dwCallback, dwCallbackInst, fdwOpen );
-
- When using this with the following class,
-
- class MCIResponse
- {
- private :
- LPHWAVEIN _waveIn ; // handle to the input device
- UINT _devID ; // identifier of the device
- WAVEFORMAT _wf ; // address of structure with device format
-
- protected :
- static void CALLBACK WaveProc( ... ) ;
-
- public :
-
- .. (other methods) ..
-
- void hookUp() ;
- virtual void process( HWAVE hW, UINT msg ) ;
- } ;
-
- Hook up the function as such:
-
- void MCIResponse :: hookUp()
- {
- DWORD cbFunc, flag, instData ;
-
- cbFunc = (DWORD) MCIResponse :: WaveProc ;
- instData = (DWORD) this ; // important part!!!
- flag = CALLBACK_FUNCTION ;
-
- waveInOpen( _waveIn, _devID, &_wf, cbFunc, instData, flag ) ;
- } // MCIResponse :: hookUp
-
- And when you process the message in WaveProc, do this:
-
- void CALLBACK MCIResponse :: WaveProc( HWAVE hW, UINT msg, DWORD inst,
- DWORD P1, DWORD P2 ) ;
- {
- MCIResponse *mci = (MCIResponse*)inst ;
- if (mci == NULL)
- return ;
-
- mci->process( hW, msg ) ;
- } // MCIResponse :: WaveProc
-
- And that's it. The "this" pointer came through as data, and then you
- casted it to your class, then used the public methods to do the rest. Again,
- I haven't a clue about P1 && P2, sorry.
-
- Hope this helps,
-
- Rob
-
-
-
- _________________________________________________________________________
- Robert McInnis
- (e) rmcinnis@gate.net
- http://www.gate.net/~rmcinnis
-
- Legal stuff:
-
- All opinions are my own, unless incorrect in which case it was the work of
- my moronic alter-ego, that does get out once in a while :)
-
- Microsoft Network is prohibited from redistibuting this work in any form,
- in whole or in part. Copyright, Robert McInnis, 9th Bit Software, 1995
- License to distribute this post is available for $1,000. Posting without
- permission constitutes an agreement to these terms. Please send notices
- of violation to postmaster@microsoft.com and rmcinnis@gate.net.
-